<?php
/**
*
* 版权所有：恰维网络<qwadmin.qiawei.com>
* 作    者：寒川<hanchuan@qiawei.com>
* 日    期：2016-09-20
* 版    本：1.0.0
* 功能说明：文章控制器。
*
**/

namespace Qwadmin\Controller;
use Qwadmin\Controller\ComController;
use Vendor\Tree;

class KdController extends ComController {

	public function add(){
		
		$category = M('category')->field('id,pid,name')->order('o asc')->select();
		$tree = new Tree($category);
		$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
		$category = $tree->get_tree(0,$str,0);
		$this->assign('category',$category);//导航
		$this -> display();
	}
		
	public function index($sid=0,$p=1){
		
		$sid = intval($sid);
		$p = intval($p)>0?$p:1;
		
		$article = M('register');
		$pagesize = 20;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		if($sid){
			$where = "id=$sid";
		}else{
			$where = '';
		}
		$count = $article->where($where)->count();
		$list  = $article->where($where)->order("regtime desc")->limit($offset.','.$pagesize)->select();
		
		$page	=	new \Think\Page($count,$pagesize); 
		$page = $page->show();
        $this->assign('list',$list);	
        $this->assign('page',$page);
		$this -> display();
	}

	public function unmake($sid=0,$p=1){
		$sid = intval($sid);
		$p = intval($p)>0?$p:1;

		$article = M('register');
		$pagesize = 20;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		if($sid){
			$where = "id=$sid and jobpro=0";
		}else{
			$where = 'jobpro=0';
		}
		$count = $article->where($where)->count();
		$list  = $article->where($where)->order("regtime desc")->limit($offset.','.$pagesize)->select();

		$page	=	new \Think\Page($count,$pagesize);
		$page = $page->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this -> display();
	}

	public function making($sid=0,$p=1){
		$sid = intval($sid);
		$p = intval($p)>0?$p:1;

		$article = M('register');
		$pagesize = 20;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		if($sid){
			$where = "id=$sid and jobpro=1";
		}else{
			$where = 'jobpro=1';
		}
		$count = $article->where($where)->count();
		$list  = $article->where($where)->order("regtime desc")->limit($offset.','.$pagesize)->select();

		$page	=	new \Think\Page($count,$pagesize);
		$page = $page->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this -> display();
	}

	public function made($sid=0,$p=1){
		$sid = intval($sid);
		$p = intval($p)>0?$p:1;

		$article = M('register');
		$pagesize = 20;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		if($sid){
			$where = "id=$sid and jobpro=2";
		}else{
			$where = 'jobpro=2';
		}
		$count = $article->where($where)->count();
		$list  = $article->where($where)->order("regtime desc")->limit($offset.','.$pagesize)->select();

		$page	=	new \Think\Page($count,$pagesize);
		$page = $page->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this -> display();
	}
	
	public function del(){
		
		$aids = isset($_REQUEST['aids'])?$_REQUEST['aids']:false;
		if($aids){
			if(is_array($aids)){
				$aids = implode(',',$aids);
				$map['id']  = array('in',$aids);
			}else{
				$map = 'id='.$aids;
			}
			if(M('register')->where($map)->delete()){
				addlog('删除报修，AID：'.$aids);
				$this->success('恭喜，报修删除成功！');
			}else{
				$this->error('参数错误！');
			}
		}else{
			$this->error('参数错误！');
		}

	}
	
	public function edit($aid){
		
		$aid = intval($aid);
		$article = M('register')->where('id='.$aid)->find();
		if($article){
			$this->assign('article',$article);
		}else{
			$this->error('参数错误！');
		}
		$this -> display();
	}
	
	public function update(){
		$aid = $_POST['aid'];
		$data['jobpro'] = isset($_POST['jobpro'])?$_POST['jobpro']:0;
		$data['fk'] = empty($_POST['fk'])?$_POST['fk']:"";
		if($aid){
			M('register')->data($data)->where('id='.$aid)->save();
			addlog('编辑，AID：'.$aid);
			$this->success('恭喜！编辑成功！',U("index"));
		}
	}
}